﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CompanyManagementDLL;
using CompanyManagementDLL.DLL;
using CompanyManagementDLL.MetaData;
using log4net;

namespace CompanyManagement.Controllers
{
    public class ModuleController : Controller
    {
        //
        // GET: /Module/

        ModuleDLL dllTask = new ModuleDLL();
        private static readonly ILog Log = LogManager.GetLogger(typeof(ModuleController));

        #region Page View

        public ActionResult Index()
        {
            if (!Utility.checkCookie())
            {
                return RedirectToAction("Index", "Login");
            }
            Utility.setCookiValue();
            List<ModuleMetaData> userlist = new List<ModuleMetaData>();
            try
            {
                userlist = dllTask.GetModuleList();
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return View(userlist);
        }

        public ActionResult Create()
        {
            if (!Utility.checkCookie())
            {
                return RedirectToAction("Index", "Login");
            }
            return View();
        }

        public ActionResult Edit(int id)
        {
            if (!Utility.checkCookie())
            {
                return RedirectToAction("Index", "Login");
            }
            ModuleMetaData ModuleMetaData = new ModuleMetaData();
            try
            {
                ModuleMetaData = dllTask.GetModuleByID(id);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return View(ModuleMetaData);
        }

        #endregion

        #region DML Operation

        [HttpPost]
        public ActionResult Create(ModuleMetaData modulemetadata)
        {
            ModuleMetaData ModuleMetaData = new ModuleMetaData();
            try
            {
                if (ModelState.IsValid)
                {
                    modulemetadata.CreatedBy = Utility.getUserID();
                    dllTask.InsertModule(modulemetadata);
                }
                else
                {
                    return View(modulemetadata);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                return View(modulemetadata);
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Edit(ModuleMetaData modulemetadata)
        {
            ModuleMetaData ModuleMetaData = new ModuleMetaData();
            try
            {
                if (ModelState.IsValid)
                {
                    dllTask.UpdateModule(modulemetadata);
                }
                else
                {
                    return View(modulemetadata);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                return View(modulemetadata);
            }
            return RedirectToAction("Index");
        }

        public ActionResult Delete(int id)
        {
            try
            {
                dllTask.DeleteModule(id);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return RedirectToAction("Index");
        }
        #endregion

    }
}
